//
//  TreeView.m
//  DemoTree
//
//  Created by 尤维维 on 2017/11/10.
//  Copyright © 2017年 EmicNet. All rights reserved.
//

#import "TreeView.h"
#import "TreeNodeModel.h"
#import "TreeViewCell.h"
#import "HeadView.h"

@interface TreeView()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) HeadView *header;

@end

@implementation TreeView

- (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate{
    if (self = [super initWithFrame:frame]) {
        self.delegate = delegate;
        self = [self initWithFrame:frame];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:self.tableView];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.tableFooterView = [UIView new];
        self.tableView.backgroundColor = [UIColor whiteColor];
        
        self.header = [[HeadView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, [self.delegate tableViewHeadHeight])];
        __weak TreeView *weakSelf = self;
        self.header.tapClickBlock = ^{
            weakSelf.model.open = !weakSelf.model.open;
            [weakSelf.tableView reloadData];
            weakSelf.reloadBlock?weakSelf.reloadBlock():nil;
        };
        self.tableView.tableHeaderView = self.header;
        
    }
    return self;
}

- (void (^)(void))reloadBlock {
    if (_reloadBlock) {
        return _reloadBlock;
    }
    __weak TreeView *weakSelf = self;
    _reloadBlock = ^(){
        [weakSelf reload];
    };
    return _reloadBlock;
}

- (void)setModel:(TreeNodeModel *)model {
    _model = model;
    self.header.title = model.name;
    self.header.level = model.level;
    self.items = model.items;
    self.tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [model height:[self.delegate tableViewCellHeight] headHeight:[self.delegate tableViewHeadHeight]]);
}

- (void)setItems:(NSArray *)items {
    _items = items;
    if (!self.model) {
        self.tableView.tableHeaderView = nil;
    }
    self.tableView.scrollEnabled = self.model?NO:YES;
    [self.tableView reloadData];
}


- (void)reload {
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.items[indexPath.row] isKindOfClass:[TreeNodeModel class]]){
        TreeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID2"];
        if (cell == nil) {
            cell = [[TreeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID2" delegate:self.delegate];
        }
        cell.model = self.items[indexPath.row];
        cell.reloadBlock = ^{
            [self.tableView reloadData];
            self.reloadBlock?self.reloadBlock():nil;
        };
        return cell;
    }
    else{
        id model = self.items[indexPath.row];
        return [self.delegate tableView:tableView cellOfTheIndexPathModel:model];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.items[indexPath.row] isKindOfClass:[TreeNodeModel class]]){
        TreeNodeModel *model = self.items[indexPath.row];
        return [model height:[self.delegate tableViewCellHeight] headHeight:[self.delegate tableViewHeadHeight]];
    }
    else{
        return [self.delegate tableViewCellHeight];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([(id)self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:model:)]) {
        [self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath model:self.items[indexPath.row]];
    }
}


@end

